﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string ProductSaleBack_D_Add(ProductSaleBack_D model, int ACID);


        [OperationContract]
        string ProductSaleBack_D_Update(ProductSaleBack_D model, int ACID);


        [OperationContract]
        string ProductSaleBack_DByzid_Delete(string zid, int ACID);


        [OperationContract]
        ProductSaleBack_D ProductSaleBack_DByzid_Get(string zid, int ACID);


        [OperationContract]
        List<ProductSaleBack_D> ProductSaleBack_D_Get(string sortBy, int ACID);

        /// <summary>
        /// 通过主表查询子表明细
        /// </summary>
        /// <param name="sortBy"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        [OperationContract]
        List<ProductSaleBack_D> ProductSaleBack_DList_ByMdjn(string djn, int ACID);

    }
    public partial class Service : IService
    {
        #region  添加ProductSaleBack_D表数据
        /// <summary>
        /// 添加ProductSaleBack_D
        /// </summary>
        /// <param name="model">ProductSaleBack_D实体</param>
        /// <returns>空或错误</returns>
        public string ProductSaleBack_D_Add(ProductSaleBack_D model, int ACID)
        {
            if (string.IsNullOrEmpty(model.djn))
            {
                return "『单据号』不能为空！";
            }
            if (model.goodscode == null)
            {
                return "『产品编号』不能为空！";
            }
            if (model.count == null || model.count == 0)
            {
                return "『退货数量』不能为空！";
            }
            if (model.backamount == null || model.backamount == 0)
            {
                return "『退货金额』不能为空！";
            }

            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSaleBack_D.Count(p => p.zid == model.zid) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.ProductSaleBack_D.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加ProductSaleBack_D表数据事物用重构
        /// <summary>
        /// 添加ProductSaleBack_D事物用重构
        /// </summary>
        /// <param name="model">ProductSaleBack_D实体</param>
        /// <returns>空或错误</returns>
        private string ProductSaleBack_D_Add(ProductSaleBack_D model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.djn))
            {
                return "『单据号』不能为空！";
            }
            if (model.goodscode == null)
            {
                return "『产品编号』不能为空！";
            }
            if (model.count == null || model.count == 0)
            {
                return "『退货数量』不能为空！";
            }
            if (model.backamount == null || model.backamount == 0)
            {
                return "『退货金额』不能为空！";
            }
            if (ctx.ProductSaleBack_D.Count(p => p.zid == model.zid) > 0)
            {
                return "主键已存在！";
            }
            ctx.ProductSaleBack_D.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion

        #region  修改ProductSaleBack_D表数据
        /// <summary>
        /// 修改ProductSaleBack_D
        /// </summary>
        /// <param name="model">ProductSaleBack_D实体</param>
        /// <returns>空或错误</returns>
        public string ProductSaleBack_D_Update(ProductSaleBack_D model, int ACID)
        {
            //if (string.IsNullOrEmpty(model.zid))
            //{
            //    return "『单据号』不能为空！";
            //}
            //if (model.isfinish == null)
            //{
            //    return "『付清标志』不能为空！";
            //}
            //if (model.ischeck == null)
            //{
            //    return "『复核标志』不能为空！";
            //}
            //if (model.discount == null)
            //{
            //    return "『折让金额』不能为空！";
            //}
            //if (model.balance == null)
            //{
            //    return "『余额』不能为空！";
            //}
            //if (model.total == null)
            //{
            //    return "『总金额』不能为空！";
            //}
            //if (model.ClientID == null || model.ClientID.Equals(Guid.Empty))
            //{
            //    return "『供应商』不能为空！";
            //}
            //if (string.IsNullOrEmpty(model.WarehouseID))
            //{
            //    return "『仓库ID』不能为空！";
            //}
            //if (model.ozid == null || model.ozid.Equals(Guid.Empty))
            //{
            //    return "『开单员』不能为空！";
            //}
            //if (model.Pdate == null)
            //{
            //    return "『开单日期』不能为空！";
            //}
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    //        if (ctx.ProductSaleBack_D.Count(p => p.zid == model.zid) != 1)
                    //        {
                    //            return "未找到指定记录或记录不唯一！";
                    //        }
                    //        ProductSaleBack_D ProductSaleBack_Dmodel = ctx.ProductSaleBack_D.Single(p => p.zid == model.zid);
                    //        if (!string.IsNullOrEmpty(model.zid))
                    //        {
                    //            ProductSaleBack_Dmodel.zid = model.zid;
                    //        }
                    //        if (model.Pdate != null)
                    //        {
                    //            ProductSaleBack_Dmodel.Pdate = model.Pdate;
                    //        }
                    //        if (model.ozid != null || model.ozid.Equals(Guid.Empty))
                    //        {
                    //            ProductSaleBack_Dmodel.ozid = model.ozid;
                    //        }
                    //        if (!string.IsNullOrEmpty(model.WarehouseID))
                    //        {
                    //            ProductSaleBack_Dmodel.WarehouseID = model.WarehouseID;
                    //        }
                    //        if (!string.IsNullOrEmpty(model.orderid))
                    //        {
                    //            ProductSaleBack_Dmodel.orderid = model.orderid;
                    //        }
                    //        if (model.ClientID != null || model.ClientID.Equals(Guid.Empty))
                    //        {
                    //            ProductSaleBack_Dmodel.ClientID = model.ClientID;
                    //        }
                    //        if (!string.IsNullOrEmpty(model.linkman))
                    //        {
                    //            ProductSaleBack_Dmodel.linkman = model.linkman;
                    //        }
                    //        if (!string.IsNullOrEmpty(model.tel))
                    //        {
                    //            ProductSaleBack_Dmodel.tel = model.tel;
                    //        }
                    //        if (model.total != null)
                    //        {
                    //            ProductSaleBack_Dmodel.total = model.total;
                    //        }
                    //        if (model.balance != null)
                    //        {
                    //            ProductSaleBack_Dmodel.balance = model.balance;
                    //        }
                    //        if (model.discount != null)
                    //        {
                    //            ProductSaleBack_Dmodel.discount = model.discount;
                    //        }
                    //        if (model.JSemzid != null || model.JSemzid.Equals(Guid.Empty))
                    //        {
                    //            ProductSaleBack_Dmodel.JSemzid = model.JSemzid;
                    //        }
                    //        if (model.ischeck != null)
                    //        {
                    //            ProductSaleBack_Dmodel.ischeck = model.ischeck;
                    //        }
                    //        if (model.Checkemzid != null || model.Checkemzid.Equals(Guid.Empty))
                    //        {
                    //            ProductSaleBack_Dmodel.Checkemzid = model.Checkemzid;
                    //        }
                    //        if (model.isfinish != null)
                    //        {
                    //            ProductSaleBack_Dmodel.isfinish = model.isfinish;
                    //        }
                    //        if (!string.IsNullOrEmpty(model.Remarks))
                    //        {
                    //            ProductSaleBack_Dmodel.Remarks = model.Remarks;
                    //        }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除ProductSaleBack_D表Byzid数据
        /// <summary>
        /// 删除ProductSaleBack_DByzid
        /// </summary>
        /// <param name="zid">单据号</param>
        /// <returns>空或错误</returns>
        public string ProductSaleBack_DByzid_Delete(string zid, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSaleBack_D.Count(p => p.zid.ToString() == zid) != 1)
                    {
                        return null;
                    }
                    ProductSaleBack_D model = ctx.ProductSaleBack_D.Single(p => p.zid.ToString() == zid);
                    ctx.ProductSaleBack_D.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除ProductSaleBack_D表Byzid数据[事物用重构]
        /// <summary>
        /// 删除ProductSaleBack_DByzid[事物用重构]
        /// </summary>
        /// <param name="zid">单据号</param>
        /// <returns>空或错误</returns>
        private string ProductSaleBack_DByzid_Delete(string zid, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (ctx.ProductSaleBack_D.Count(p => p.zid.ToString() == zid) != 1)
            {
                return null;
            }
            ProductSaleBack_D model = ctx.ProductSaleBack_D.Single(p => p.zid.ToString() == zid);
            ctx.ProductSaleBack_D.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取ProductSaleBack_D表Byzid数据
        /// <summary>
        /// 获取ProductSaleBack_DByzid
        /// </summary>
        /// <param name="zid">单据号</param>
        /// <returns>当前表</returns>
        public ProductSaleBack_D ProductSaleBack_DByzid_Get(string zid, int ACID)
        {
            if (string.IsNullOrEmpty(zid))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.ProductSaleBack_D.Count(p => p.zid.ToString() == zid) != 1)
                    {
                        return null;
                    }
                    return ctx.ProductSaleBack_D.SingleOrDefault(p => p.zid.ToString() == zid);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取ProductSaleBack_D表数据
        /// <summary>
        /// 获取ProductSaleBack_D
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<ProductSaleBack_D></returns>
        public List<ProductSaleBack_D> ProductSaleBack_D_Get(Expression<Func<ProductSaleBack_D, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSaleBack_D.ToList();
                        }
                        else
                        {
                            return ctx.ProductSaleBack_D.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSaleBack_D.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.ProductSaleBack_D.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取ProductSaleBack_D表数据
        /// <summary>
        /// 获取ProductSaleBack_D
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<ProductSaleBack_D></returns>
        public List<ProductSaleBack_D> ProductSaleBack_D_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return ProductSaleBack_D_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取ProductSaleBack_D表数据计数
        /// <summary>
        /// 获取ProductSaleBack_D表数据计数
        /// </summary>
        /// <returns>List<ProductSaleBack_D></returns>
        public int ProductSaleBack_D_GetCount(Expression<Func<ProductSaleBack_D, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.ProductSaleBack_D.Count();

                    }
                    else
                    {
                        return ctx.ProductSaleBack_D.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取ProductSaleBack_D分页表数据
        /// <summary>
        /// 获取ProductSaleBack_D分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<ProductSaleBack_D></returns>
        public List<ProductSaleBack_D> ProductSaleBack_D_Getpage(int pageIndex, int pageSize, Expression<Func<ProductSaleBack_D, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSaleBack_D.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.ProductSaleBack_D.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.ProductSaleBack_D.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.ProductSaleBack_D.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改ProductSaleBack_D表数据
        /// <summary>
        /// 批量修改ProductSaleBack_D
        /// </summary>
        /// <param name="predicate">Expression<Func<ProductSaleBack_D, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<ProductSaleBack_D,ProductSaleBack_D>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int ProductSaleBack_D_batchUpdate(Expression<Func<ProductSaleBack_D, bool>> predicate, Expression<Func<ProductSaleBack_D, ProductSaleBack_D>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.ProductSaleBack_D.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改ProductSaleBack_D表数据事物用重构
        /// <summary>
        /// 批量修改ProductSaleBack_D事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<ProductSaleBack_D, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<ProductSaleBack_D,ProductSaleBack_D>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int ProductSaleBack_D_batchUpdate(Expression<Func<ProductSaleBack_D, bool>> predicate, Expression<Func<ProductSaleBack_D, ProductSaleBack_D>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.ProductSaleBack_D.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除ProductSaleBack_D表数据
        /// <summary>
        /// 批量删除ProductSaleBack_D
        /// </summary>
        /// <param name="predicate">Expression<Func<ProductSaleBack_D, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int ProductSaleBack_D_batchDel(Expression<Func<ProductSaleBack_D, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.ProductSaleBack_D.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除ProductSaleBack_D表数据事物用重构
        /// <summary>
        /// 批量删除ProductSaleBack_D事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<ProductSaleBack_D, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int ProductSaleBack_D_batchDel(Expression<Func<ProductSaleBack_D, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.ProductSaleBack_D.Delete(predicate);
            return ok;
        }
        #endregion




        public List<ProductSaleBack_D> ProductSaleBack_DList_ByMdjn(string djn, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext cxt = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<ProductSaleBack_D, bool>> predicate = PredicateBuilder.True<ProductSaleBack_D>();
                predicate = p => p.djn == djn; //销售单据号
                return cxt.ProductSaleBack_D.Where(predicate).ToList();
            }
        }
    }
}

